package org.jsbd.boss.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.dao.IBaseInfoDAO;
import org.jsbd.boss.dao.IUseStatisticsDAO;
import org.jsbd.boss.domian.BaseInfo;
import org.jsbd.boss.domian.statistics.UseStatisticsDto;
import org.jsbd.boss.service.IUseStatisticsService;
import org.jsbd.boss.util.BigDecimalUtil;
import org.jsbd.boss.util.ProvinceUtils;
import org.springframework.stereotype.Service;

@Service("useStatisticsService")
public class UseStatisticsServiceImpl implements IUseStatisticsService {

	@Resource
	private IUseStatisticsDAO useStatisticsDAO;
	@Resource
	private IBaseInfoDAO baseInfoDAO;
	
	@Override
	public Map<String, Object> findBaseListWithPage(DataGridModel page, UseStatisticsDto usDTO) {
		Map<String, Object> map = useStatisticsDAO.findBaseListWithPage(page, usDTO);
		List<UseStatisticsDto> usDTOs  = (List<UseStatisticsDto>) map.get("rows");
		List<BaseInfo> baseInfos = baseInfoDAO.getAllChannelInfo();
		
		Map<String, String> biMap = new HashMap<String, String>();
		for (BaseInfo baseInfo : baseInfos) {
			biMap.put(baseInfo.getId(), baseInfo.getBaseName());
		}
		
		for (UseStatisticsDto u : usDTOs) {
			String baseId = String.valueOf(u.getBase());
			u.setBaseName(biMap.get(baseId));
		}
		
		return map;
	}

	@Override
	public Map<String, Object> findProvinceListWithPage(DataGridModel page, UseStatisticsDto usDTO) {
		Map<String, Object> map = useStatisticsDAO.findPrivinceListWithPage(page, usDTO);
		List<UseStatisticsDto> usDTOs  = (List<UseStatisticsDto>) map.get("rows");
		for (UseStatisticsDto u : usDTOs) {
			String provinceId = String.valueOf(u.getProvinceId());
			u.setProvinceName(ProvinceUtils.getProvinceName(provinceId));
		}
		return map;
	}

	@Override
	public Map<String, Object> selectRecords(UseStatisticsDto usDTO) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<UseStatisticsDto> usDtos = useStatisticsDAO.selectRecords(usDTO);
		if (usDtos.size() == 0) {
			return map;
		}
		
		List<String> xAxis = new ArrayList<String>();
		for (UseStatisticsDto us : usDtos) {
			xAxis.add(us.getDay());
		}
		List<Integer> totalNums = useStatisticsDAO.selectTotalNum(xAxis);
		for (int i =0; i < usDtos.size(); i++) {
			float rate = BigDecimalUtil.divideToIntegralValue(usDtos.get(i).getNum(), totalNums.get(i));
			usDtos.get(i).setRate(rate);
		}
		
		map.put("xAxis", xAxis);
		map.put("yAxis", usDtos);
		return map;
	}

	@Override
	public Map<String, Object> selectPartRecords(UseStatisticsDto usDTO) {
		Map<String, Object> map = new HashMap<String, Object>();
		usDTO.setBase(0);
		List<UseStatisticsDto> usDtos0 = useStatisticsDAO.selectPartRecords(usDTO);
		usDTO.setBase(3);
		List<UseStatisticsDto> usDtos3 = useStatisticsDAO.selectPartRecords(usDTO);
		usDTO.setBase(6);
		List<UseStatisticsDto> usDtos6 = useStatisticsDAO.selectPartRecords(usDTO);
		
		List<String> xAxis = new ArrayList<String>();
		for (UseStatisticsDto us : usDtos0) {
			xAxis.add(us.getDay());
		}
		List<Integer> totalNums = useStatisticsDAO.selectTotalNum(xAxis);
		for (int i =0; i < usDtos0.size(); i++) {
			float rate = BigDecimalUtil.divideToIntegralValue(usDtos0.get(i).getNum(), totalNums.get(i));
			usDtos0.get(i).setRate(rate);
		}
		
		map.put("xAxis", xAxis);
		map.put("yAxis", usDtos0);
		return map;
	}

}
